package com.gxuwz.attend.service.instructor;

import com.gxuwz.attend.dao.instructor.ExportDao;
import com.gxuwz.attend.entity.Leave;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ExportService {
    private ExportDao exportDao;
    private static final Logger LOGGER = Logger.getLogger(ExportService.class.getName());

    public void setExportDao(ExportDao exportDao) {
        this.exportDao = exportDao;
    }

    // 根据学期和辅导员工号获取请假记录
    public List<Leave> getLeave(String term, String instID) {
        return exportDao.getLeave(term, instID);
    }

    // 导出 Excel 功能
    public void exportToExcel(List<Leave> leaveList, String filePath) {
        Workbook workbook = null;
        FileOutputStream fileOut = null;

        try {
            // 创建 Excel 工作簿
            workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Leave Requests");

            // 设置表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("请假ID");
            headerRow.createCell(1).setCellValue("课程ID");
            headerRow.createCell(2).setCellValue("请假原因");
            headerRow.createCell(3).setCellValue("请假天数");
            headerRow.createCell(4).setCellValue("学号");
            headerRow.createCell(5).setCellValue("请假状态");
            headerRow.createCell(6).setCellValue("申请时间");
            headerRow.createCell(7).setCellValue("审核日期");

            // 填充数据
            int rowNum = 1;
            for (Leave leave : leaveList) {
                Row dataRow = sheet.createRow(rowNum++);
                dataRow.createCell(0).setCellValue(leave.getLeaveID());
                dataRow.createCell(1).setCellValue(leave.getCourseID());
                dataRow.createCell(2).setCellValue(leave.getReason());
                dataRow.createCell(3).setCellValue(leave.getDaynum());
                dataRow.createCell(4).setCellValue(leave.getStuNo());
                dataRow.createCell(5).setCellValue(leave.getOpinion());

                // 处理申请时间
                String applyTime = leave.getApplytime();
                dataRow.createCell(6).setCellValue((applyTime != null && !applyTime.isEmpty()) ? applyTime : "无");

                // 处理审核日期
                String auditTime = leave.getAudittime();
                dataRow.createCell(7).setCellValue((auditTime != null && !auditTime.isEmpty()) ? auditTime : "无");
            }

            // 输出 Excel 文件
            fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            LOGGER.info("Excel文件已成功保存到: " + filePath);

        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "Error while writing Excel file to " + filePath, e);
        } finally {
            try {
                if (fileOut != null) {
                    fileOut.close();
                }
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Error while closing resources", e);
            }
        }
    }
}
